package com.juanfrivaldes.cio2005.web;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.juanfrivaldes.cio2005.business.Cio2005Facade;

/**
 * Superclass for Struts actions in this application. Looks up the Spring
 * WebApplicationContext via the ServletContext, and obtains the PetStoreFacade
 * implementation from it, making it available to subclasses via a protected
 * getter method.
 */
public abstract class BaseAction extends Action {

	private Cio2005Facade cio2005;
	
	private static Log log = LogFactory.getLog(AutentificacionUsuarioAction.class); 

	public void setServlet(ActionServlet actionServlet) {
		super.setServlet(actionServlet);
		ServletContext servletContext = actionServlet.getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(servletContext);
		this.cio2005 = (Cio2005Facade) wac.getBean("cio2005");
	}


	protected abstract ActionForward protectedExecute(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		return protectedExecute(mapping, form, request, response);

	}

	/**
	 * @return Returns the cio2005.
	 */
	public Cio2005Facade getCio2005() {
		return cio2005;
	}
}
